package com.example.administrator.daydayappmarket.software.category.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.software.category.model.SoftwareCategoryBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by lenovo on 2016/12/27.
 */

public class SoftwareCategoryAdapter extends BaseAdapter {

    private Context context;
    private List<SoftwareCategoryBean> data;

    public SoftwareCategoryAdapter(Context context, List<SoftwareCategoryBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SoftwareCategoryBean getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_software_category_gridview, parent, false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        SoftwareCategoryBean softwareCategoryBean = data.get(position);
        Glide.with(context).load(Contacts.FRONT_IMG+softwareCategoryBean.getImage()).into(viewHolder.imageView);
        viewHolder.textView.setText(softwareCategoryBean.getTitle());

        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.item_software_category_imageView)
        ImageView imageView;
        @InjectView(R.id.item_software_category_textView)
        TextView textView;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
